using System;

namespace RenameX.Rules
{
    public class SequenceRule : RuleBase
    {
        public override string Name => "序列化";

        private int _start = 1;
        public int Start { get => _start; set => SetProperty(ref _start, value); }

        private int _step = 1;
        public int Step { get => _step; set => SetProperty(ref _step, value); }

        private int _padWidth = 2;
        public int PadWidth { get => _padWidth; set => SetProperty(ref _padWidth, value); }

        private string _format = "{0}";
        public string Format { get => _format; set => SetProperty(ref _format, value); }

        public enum InsertPosition { Prefix, Suffix }
        private InsertPosition _position = InsertPosition.Suffix;
        public InsertPosition Position { get => _position; set => SetProperty(ref _position, value); }

        private int _currentIndex;

        public void Reset()
        {
            _currentIndex = Start;
        }

        public override string Apply(string originalName)
        {
            if (_currentIndex == 0)
            {
                _currentIndex = Start;
            }

            var numberText = _currentIndex.ToString().PadLeft(Math.Max(1, PadWidth), '0');
            var formatted = string.Format(Format, numberText);
            _currentIndex += Step;

            return Position == InsertPosition.Prefix
                ? string.Concat(formatted, originalName)
                : string.Concat(originalName, formatted);
        }
    }
}


